package com.zhiyou100.dao;

import com.zhiyou100.entity.User;
import com.zhiyou100.tools.mysql.C3P0Tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author yang
 * @className UserDao
 * @packageName com.zhiyou100.dao
 * @Description TODO 用户 dao 层
 * @date 2020-07-31 11:23
 */
public class UserDao {

    // 功能 登陆功能：注册：个人资料修改

    public User getOneByUid(int uid) {

        Connection con = C3P0Tools.getCon();
        PreparedStatement pre = null;
        ResultSet set = null;
        User u = null;
        try {
            pre = con.prepareStatement("select * from user where uid=?");
            pre.setInt(1, uid);
            set = pre.executeQuery();
            while (set.next()) {
//				Integer uid, String uname, String upwd, Character usex,
//				String urealName, String uphoto, String uphone,
//				Integer uscore, String uenjoy
                String uname = set.getString("uname");
                String upwd = set.getString("upwd");
                char usex = set.getString("usex").charAt(0);
                String urealName = set.getString("ureal_name");
                String uphoto = set.getString("uphoto");
                String uphone = set.getString("uphone");
                int uscore = Integer.parseInt(set.getString("uscore"));
                String uenjoy = set.getString("uenjoy");
                u = new User(uid, uname, upwd, usex, urealName, uphoto, uphone, uscore, uenjoy);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        C3P0Tools.close(set, pre, con);
        return u;
    }

    public User getOneByUname(String uname) {
        Connection con = C3P0Tools.getCon();
        PreparedStatement pre = null;
        ResultSet set = null;
        User u = null;
        try {
            pre = con.prepareStatement("select * from user where uname=?");
            pre.setString(1, uname);
            set = pre.executeQuery();
            while (set.next()) {
//				Integer uid, String uname, String upwd, Character usex,
//				String urealName, String uphoto, String uphone,
//				Integer uscore, String uenjoy
                int uid = set.getInt("uid");
                String upwd = set.getString("upwd");
                char usex = set.getString("usex").charAt(0);
                String urealName = set.getString("ureal_name");
                String uphoto = set.getString("uphoto");
                String uphone = set.getString("uphone");
                int uscore = Integer.parseInt(set.getString("uscore"));
                String uenjoy = set.getString("uenjoy");
                u = new User(uid, uname, upwd, usex, urealName, uphoto, uphone, uscore, uenjoy);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        C3P0Tools.close(set, pre, con);
        return u;
    }

    public int addOne(User u) {
        Connection con = C3P0Tools.getCon();
        PreparedStatement pre = null;
        int hang;
        try {
//			Integer uid, String uname, String upwd, Character usex,
//			String urealName, String uphoto, String uphone,
//			Integer uscore, String uenjoy
            pre = con.prepareStatement("insert into user("
                    + "uname," + "upwd," + "ureal_name," + "usex,"
                    + "uphoto,"
                    + "uphone,"
                    + "uscore,"
                    + "uenjoy) values(?,?,?,?,?,?,?,?)");
            pre.setString(1, u.getUname());
            pre.setString(2, u.getUpwd());
            pre.setString(3, u.getUrealName());
            pre.setString(4, u.getUsex() + "");
            pre.setString(5, u.getUphoto());
            pre.setString(6, u.getUphone());
            pre.setFloat(7, u.getUscore());
            pre.setString(8, u.getUenjoy());
            hang = pre.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        C3P0Tools.close(null, pre, con);
        return hang;
    }

    public int updateOne(User u) {
        Connection con = C3P0Tools.getCon();
        PreparedStatement pre = null;
        int hang;
        try {
//			Integer uid, String uname, String upwd, Character usex,
//			String urealName, String uphoto, String uphone,
//			Integer uscore, String uenjoy
            pre = con.prepareStatement("update user set uname=?,"
                    + "upwd=?,"
                    + "ureal_name=?,"
                    + "usex=?,"
                    + "uphone=?,"
                    + "uphoto=?,"
                    + "uscore=?,"
                    + "uenjoy=? where uid=?");
            pre.setString(1, u.getUname());
            pre.setString(2, u.getUpwd());
            pre.setString(3, u.getUrealName());
            pre.setString(4, u.getUsex() + "");
            pre.setString(5, u.getUphone());
            pre.setString(6, u.getUphoto());
            pre.setFloat(7, u.getUscore());
            pre.setString(8, u.getUenjoy());
            pre.setInt(9, u.getUid());
            hang = pre.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        C3P0Tools.close(null, pre, con);
        return hang;
    }

    public static void main(String[] args) {
        UserDao dao = new UserDao();
        //System.out.println(dao.getOneByUname("hmm"));
        //System.out.println(dao.addOne(new User("test", "113", '妖', "妖精", "2.jpg", "110", 100, "跑步")));
        System.out.println(dao.updateOne(new User(3, "test3", "1133", '妖', "妖精3", "3.jpg", "1103", 100, "跑步3")));
    }

}
